package com.talking.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.talking.App;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Pattern;

public class Tools {
    public static boolean passwordCorrect(String password){
        return password.matches("^(?=.*\\d)(?=.*[a-zA-Z])[\\da-zA-Z!@#$%^&*()_+-=;:'|{},<.>/?`~]{6,20}+$");
    }
    public static void toast(String str) {
        Toast.makeText(App.instance, str, Toast.LENGTH_LONG).show();
    }
    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) return false;
        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        return p.matcher(email).matches();
    }
    public static boolean isWifiProxy(){
        String proxyAddress = System.getProperty("http.proxyHost");
        String portStr = System.getProperty("http.proxyPort");
        int proxyPort = Integer.parseInt((portStr != null ? portStr : "-1"));
        return (!TextUtils.isEmpty(proxyAddress))&&(proxyPort!=-1);
    }
    public static boolean hasExternalStoragePermission(Context context){
        return context.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") == PackageManager.PERMISSION_GRANTED;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void requestPermission(@NotNull Activity activity) {
        activity.requestPermissions(new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, Manifest.permission.MANAGE_EXTERNAL_STORAGE,Manifest.permission.CAMERA
        }, 0);
    }

    public static void saveBitmap(String path, String name, Bitmap bitmap) {
        try {
            FileOutputStream saveImgOut = new FileOutputStream(new File(path, name));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, saveImgOut);
            saveImgOut.flush();
            saveImgOut.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static String createRandom() {
        return String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
    }
    public static RequestOptions getGlideOption(){
//        return RequestOptions.bitmapTransform(new CircleCrop()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE);
        return RequestOptions.bitmapTransform(new CircleCrop());//.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE);
    }
}
